package com.example.project.common.advice;

import com.example.project.common.tools.AppException;
import com.example.project.common.tools.R;
import com.example.project.common.tools.ResponseEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.SignatureException;

/**
 * @author: Jerry.Zhang
 * @date: 2022/3/21 14:58
 */
@ControllerAdvice
@ResponseBody
public class ControllerExceptionAdvice {


    /**
     * 自定义异常
     * @param ex
     * @param request
     * @return
     */
    @ExceptionHandler(AppException.class)
    public R appExceptionHandler(AppException ex, HttpServletRequest request) {
        System.out.println(ex.getCode()+":"+ex.getMessage());
        ex.printStackTrace();
        return new R(ex.getCode(), ex.getMessage(), null);
    }

    /**
     * 如果抛出的是Exception 的增强
     */
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(Exception ex, HttpServletRequest request) {
        ex.printStackTrace();
        return new R(ResponseEnum.UNKNOW_ABNORMAL, null);
    }

}
